function showAnswer(data, text_status, jqxhr){
	if(data.charAt(0) == '{'){
		showMessage(data);
		return;
	}
	
	var question_key = new Array();
	
	data = data.split('\n');
	for(var i = 0; i < data.length; i++){
		var line = data[i].split(':');
		if(line.length != 2){
			continue;
		}
		question_key[line[0]] = line[1];
	}
	
	//Result Animation Queue
	if(question_key.length > 1){
		$("html,body").animate({scrollTop: 0}, 1000);
	}
	var anime_queue = new Array();

	var final_score = 0;
	
	//Judge Each Question
	$('.box').each(function(){
		var id = $(this).find('input')[0].id.split("_")[1].split("[")[0];
		var correct = true;
		
		if(typeof question_key[id] == 'undefined'){
			correct = false;
		}else{
			var my_answer = '';
			$(this).find('input').each(function(){
				var opt;
				if($(this).attr('type') == 'radio'){
					opt = $(this).val();
				}else{
					opt = $(this).attr('id').split('[')[1].split(']')[0];
				}
				var ch = 0x41 + parseInt(opt, 10);
				ch = unescape('%u00' + ch.toString(16));
				
				if($(this).attr('checked') == 'checked'){
					my_answer += ch;
				}

				if(($(this).attr('checked') == 'checked') != (question_key[id].indexOf(ch) >= 0)){
					correct = false;
					return;
				}
			});
		}

		var size = 130;
		var top = $(this).offset().top + $(this).outerHeight() - size;
		var left = $(this).offset().left + $(this).outerWidth() - size;
		anime_queue.push(function(){
			var anime = createFlashObject('./media/' + (correct ? 'Tick' : 'Cross') + 'Animated.swf');
			anime.css('position', 'absolute');
			anime.css('width', size).css('height', size);
			anime.css('top', top);
			anime.css('left', left);
			$('body').append(anime);
		});
		
		var height = $(this).outerHeight();
		anime_queue.push(function(){
			$("html,body").animate({scrollTop: '+=' + height}, 1200);
		});
		
		if(correct && $(this).find('.point').length == 1){
			final_score += parseInt($(this).find('.point').html(), 10);
		}
	});

	//Pop the final slide down action, no need
	anime_queue.pop();
	
	if($('.point').length > 0){
		anime_queue.push(function(){
			showMessage({message:'Your final score is ' + final_score + '.'});
		});
	}
	
	$(document).queue("resultAnime", anime_queue);
	if(question_key.length > 1){
		for(var i = 1; i <= anime_queue.length; i++){
			setTimeout('$(document).dequeue("resultAnime");', 1200 * i);
		}
	}else{
		$(document).dequeue("resultAnime");
	}
}

function postAnswer(){
	if(typeof timer_interval != 'undefined'){
		clearInterval(timer_interval);
	}

	$('input').attr('disabled', 'disabled').attr('autocomplete', 'off');
	$('button').slideUp();
	
	var post_data = 'ajax=1';
	
	if($('#pid').length > 0){
		post_data += '&pid=' + parseInt($('#pid').val(), 10);
	}
	
	$('input').each(function(){
		if($(this).attr('type') == 'radio'){
			if($(this).attr('checked') == 'checked'){
				post_data += '&' + $(this).attr('name') + '=' + $(this).val();	
			}
		}else if($(this).attr('type') == 'checkbox'){
			if($(this).attr('checked') == 'checked'){
				post_data += '&' + $(this).attr('name') + '=on';
			}
		}
	});
	
	$.post('CheckAnswer', post_data, showAnswer);
}